﻿using SuperFx;
using SuperLottery.AdminPortal.Models;
using SuperLottery.Datas;
using SuperLottery.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuperLottery.AdminPortal.Controllers
{
	public class DrawController : Controller
	{
		//
		// GET: /Draw/

		public ActionResult Index(string issuer, string game)
		{
			LotteryDrawDbService dbSvc = new LotteryDrawDbService();
			List<LotteryDraw> list = dbSvc.GetLotteryDraws(100, 1);
			if (String.IsNullOrWhiteSpace(issuer) || String.IsNullOrWhiteSpace(game))
			{
				list = dbSvc.GetLotteryDraws(100, 1);
			}
			else
			{
				list = dbSvc.GetLotteryDraws(issuer, game, 100, 1);
			}

			return View(list);
		}

		//
		// GET: /Draw/IndexLatest

		public ActionResult IndexLatest()
		{
			var dmLatestLotteryDraws = LotteryDrawDbService.Instance.GetLatestLotteryDraws();
			List<DrawModel> model = dmLatestLotteryDraws.Convert2<DrawModel>();
			return View(model);
		}

		//
		// GET: /Draw/Create/

		public ActionResult Create(string issuerGame)
		{
			return View(new DrawModel() { IssuerGame = issuerGame });
		}

		//
		// POST: /Draw/Create/

		[HttpPost]
		public ActionResult Create(DrawModel model)
		{
			ModelState.Keys.Where(key => key.EndsWith("NumberOfWinners") || key.EndsWith("MoneyPerWinner"))
				.ToList().ForEach(key => ModelState.Remove(key));

			if (ModelState.IsValid)
			{
				LotteryDraw lotteryDraw = model.Convert2<LotteryDraw>();
				lotteryDraw.Issuer = model.IssuerGame.Split('-')[0];
				lotteryDraw.Game = model.IssuerGame.Split('-')[1];

				LotteryDrawDbService.Instance.Create(lotteryDraw);

				var latest = LotteryDrawDbService.Instance.GetLatestLotteryDraw(lotteryDraw.Issuer, lotteryDraw.Game);
				if (latest == null || lotteryDraw.DrawDate >= latest.DrawDate)
				{
					LotteryDrawDbService.Instance.SaveToLatest(lotteryDraw);
				}

				LotteryDrawDbService.Instance.Create(model.GetWinningDetails(), lotteryDraw.Id);

				return RedirectToAction("IndexLatest");
			}

			return View(model);
		}

		//
		// GET: /Draw/Edit/

		public ActionResult Edit(long id)
		{
			LotteryDraw lotteryDraw = LotteryDrawDbService.Instance.GetLotteryDraw(id);
			List<WinningDetail> winningDetails = LotteryDrawDbService.Instance.GetWinningDetails(id);
			DrawModel model = lotteryDraw.Convert2<DrawModel>();
			model.IssuerGame = model.GetIssuerGame();
			model.SetWinningDetails(winningDetails);
			return View(model);
		}

		//
		// POST: /Draw/Edit/

		[HttpPost]
		public ActionResult Edit(DrawModel model)
		{
			ModelState.Keys.Where(key => key.EndsWith("NumberOfWinners") || key.EndsWith("MoneyPerWinner"))
				.ToList().ForEach(key => ModelState.Remove(key));

			if (ModelState.IsValid)
			{
				LotteryDraw lotteryDraw = model.Convert2<LotteryDraw>();
				lotteryDraw.Issuer = model.IssuerGame.Split('-')[0];
				lotteryDraw.Game = model.IssuerGame.Split('-')[1];

				lotteryDraw = LotteryDrawDbService.Instance.Update(lotteryDraw);

				var latest = LotteryDrawDbService.Instance.GetLatestLotteryDraw(lotteryDraw.Issuer, lotteryDraw.Game);
				if (latest == null || lotteryDraw.DrawDate >= latest.DrawDate)
				{
					LotteryDrawDbService.Instance.SaveToLatest(lotteryDraw);
				}

				LotteryDrawDbService.Instance.Update(model.GetWinningDetails(), lotteryDraw.Id);

				return RedirectToAction("Index");
			}

			return View(model);
		}
	}
}
